---
--类名: RankingManager.lua
--创建日期: 2018-5-14
--创建人: 宋建邦
--功能描述: 

local RankingManager = class("RankingManager")

function RankingManager:ctor()
    self.serverrank = {}
    self.rewards = {}
    self:initRankingTree()
end

function RankingManager:initRankingTree()
    if self.ht then
        return
    end

    local ts = GD:getRankingTypes()   
    self.ht = {}
    self.rt = {}
    self.rt.children = {}
    for k, v in ipairs(ts) do
        if checknumber(v.ID) ~= 5 then
            local c = v:getValues()
            self.ht[checknumber(v.ID)] = c
            if checknumber(v.belong) == 0 then
                table.insert(self.rt.children, c)
            end
        end
    end

    for k, v in ipairs(ts) do
        local b = checknumber(v.belong)
        if b ~= 0 then
            self.ht[b].children = self.ht[b].children or {}
            table.insert(self.ht[b].children, self.ht[checknumber(v.ID)])
        end
    end

    --第三层 在排行榜里
    self:initRankingConfigs()

    table.sort(self.rt.children, function(a, b)
        return checknumber(a.order) < checknumber(b.order)
    end)

    for k, v in pairs(self.ht) do
        if v.children and #v.children > 1 then
            table.sort(v.children, function(a, b)
                return checknumber(a.order) < checknumber(b.order)
            end)
        end
    end
end

function RankingManager:getSpRanking()
    local fcs = {}
    for i, v in ipairs(self.ht[110].children) do
        if checknumber(v.ID) == 950 
            or checknumber(v.ID) == app:getInst("PvpModel"):getOpenChallengeRankId()
        then
            table.insert(fcs, v)
        end
    end
    return fcs
end

function RankingManager:initRankingConfigs()
    local rs = GD:getRankingConfigs()
    self.hcfg = {}
    for k, v in ipairs(rs) do
        if checknumber(v.isguild) == 0 then
            local c = v:getValues()
            self.hcfg[checknumber(v.ID)] = c

            local b = checknumber(v.belong)
            if b ~= 0 then
                self.ht[b].children = self.ht[b].children or {}
                c.title = c.name
                c.titleshow = 1
                table.insert(self.ht[b].children, c)
            end
        end
    end

    local cs = GD:getRankingConditionConfigs()
    self.hconds = {}

    for k, v in ipairs(cs) do
        local c = v:getValues()

        self.hconds[checknumber(v.ID)] = c 
    end
end

function RankingManager:getRankingReward(rid)
    local ret 
    if self.hcfg and self.hcfg[rid] then
        if self.rewards[rid] then
            return self.rewards[rid]
        end

        local rs = self.hcfg[rid].rewardShow
        if rs and rs:len() > 1 then
            ret = {}
            local ps = rs:split(";")
            for i, v in ipairs(ps) do 
                local rd = v:split("|")
                local rr = rd[1]:split("-")
                table.insert(ret, { b = checknumber(rr[1]),
                                    e = checknumber(rr[2]),
                                    d = checknumber(rd[2]),
                                    pd = UD:parseDropIDToBagItem(checknumber(rd[2]))
                                })
            end
            self.rewards[rid] = ret
        end
    end
    return ret
end

function RankingManager:hasRankingReward(rid)
    local r = self:getRankingReward(rid)
    return r and #r > 0
end

RankingManager.RankChangeEventName = "rankingchangeevent"

function RankingManager:requestRank(aID)
    if self.serverrank[aID] then
        EM:notify(RankingManager.RankChangeEventName, aID)
    end
    app.session:request("pb_rank.C2S_TargetRankList", {id=protobuf.enum_name("pb_rank.RANK_ID", aID)}, function(msg)
        local sid = protobuf.enum_id("pb_rank.RANK_ID", msg.id)
        self.serverrank[aID] = msg

        EM:notify(RankingManager.RankChangeEventName, aID)
    end, nil, function(errMsg)
    end)
end

function RankingManager:hmstr(aSeconds)
    local m = aSeconds/60
    local s = math.fmod(aSeconds, 60)

    return string.format("%d:%02d", m, s)
end

RankingManager.teamIds = {90000, 90001, 90002, 90003, 90004, 90005, 90006, 90007, 90008, 90009, 90050, 90051, 90010, 90011, 90012, 90013, 90014, 90015}
RankingManager.raceIds = {201,    202,    203,   204,   205,   201,   202,   203,   204,   205,   206,   207,   206,   207,   208,   209,   208, 209}
RankingManager.seasonIds = {90000, 90001, 90002, 90003, 90004, 90050, 90051, 90012, 90013}
RankingManager.eliteMatchIds = {20001, 20002}

function RankingManager:isSeasonRank(aID)
    -- return aID < 90005
    return table.indexof(RankingManager.seasonIds, aID)
end

function RankingManager:isTeamRank(aID)
    return table.indexof(RankingManager.teamIds, aID)
end

function RankingManager:isEliteMatchRank(aID)
    return table.indexof(RankingManager.eliteMatchIds, aID)
end

function RankingManager:rankId2raceId(aRankId)
    return RankingManager.raceIds[table.indexof(RankingManager.teamIds, aRankId)]
end

function RankingManager:raceId2rankId(aRaceId)
    return RankingManager.teamIds[table.indexof(RankingManager.raceIds, aRaceId)]
end

function RankingManager:requestTeamRank(aID)
    if self.serverrank[aID] then
        EM:notify(RankingManager.RankChangeEventName, aID)
    end
    local arg = {target = self:rankId2raceId(aID),
                seasonRank = self:isSeasonRank(aID)}
    app.session:request("pb_speeddungeon.C2S_GetSpeedDungeonRanking", arg, function(msg)
        local cmsg = clone(msg)
        --过滤一下，有时会有空数据
        local newTeams = {}
        for i, v in ipairs(cmsg.infos) do
            local newInfos = {}
            for j, k in ipairs(v.infos) do
                if k.baseInfo then
                    table.insert(newInfos, k)
                end
            end
            v.infos = newInfos
            if #newInfos > 0 then
                table.insert(newTeams, v)
            end
        end
        cmsg.infos = newTeams
        
        self.serverrank[aID] = cmsg

        EM:notify(RankingManager.RankChangeEventName, aID)
    end, nil, function(errMsg)
    end)
end

function RankingManager:requestWorldRank(aID)
    if self.serverrank[aID] then
        EM:notify(RankingManager.RankChangeEventName, aID)
    end
    app.session:request("pb_rank.C2S_WorldRankList", {id=protobuf.enum_name("pb_rank.RANK_ID", aID)}, function(msg)
        local rankInfos = {}
        rankInfos.id = rawget(msg, "id")
        rankInfos.myRank = rawget(msg, "myRank")
        rankInfos.myScore = rawget(msg, "myScore")
        local nodeInfo = {}
        local nodes = rawget(msg, "nodes")
        if nodes then
            for k, v in ipairs(nodes) do
                table.insert(nodeInfo, {
                    userBaseInfo = rawget(v, "user"),
                    stamp = rawget(v, "stamp"),
                    value = rawget(v, "value"),
                    server = rawget(v, "server"),
                })
            end
        end
        rankInfos.infos = nodeInfo
        self.serverrank[aID] = rankInfos
        EM:notify(RankingManager.RankChangeEventName, aID)
    end, nil, function(errMsg)
    end)
end

return RankingManager